<template>
  <el-dialog
    :title="!dataForm.normid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="规范描述" prop="normdescribr">
      <el-input v-model="dataForm.normdescribr" placeholder="规范描述"></el-input>
    </el-form-item>
    <el-form-item label="电流中心值" prop="curcentral">
      <el-input v-model="dataForm.curcentral" placeholder="电流中心值"></el-input>
    </el-form-item>
    <el-form-item label="电流下限偏离" prop="curdown">
      <el-input v-model="dataForm.curdown" placeholder="电流下限偏离"></el-input>
    </el-form-item>
    <el-form-item label="电流上限偏离" prop="curup">
      <el-input v-model="dataForm.curup" placeholder="电流上限偏离"></el-input>
    </el-form-item>
    <el-form-item label="电压中心值" prop="volcentral">
      <el-input v-model="dataForm.volcentral" placeholder="电压中心值"></el-input>
    </el-form-item>
    <el-form-item label="电压下限偏离" prop="voldown">
      <el-input v-model="dataForm.voldown" placeholder="电压下限偏离"></el-input>
    </el-form-item>
    <el-form-item label="电压上限偏离" prop="volup">
      <el-input v-model="dataForm.volup" placeholder="电压上限偏离"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          normid: 0,
          normdescribr: '',
          curcentral: '',
          curdown: '',
          curup: '',
          volcentral: '',
          voldown: '',
          volup: ''
        },
        dataRule: {
          normdescribr: [
            { required: true, message: '规范描述不能为空', trigger: 'blur' }
          ],
          curcentral: [
            { required: true, message: '电流中心值不能为空', trigger: 'blur' }
          ],
          curdown: [
            { required: true, message: '电流下限偏离不能为空', trigger: 'blur' }
          ],
          curup: [
            { required: true, message: '电流上限偏离不能为空', trigger: 'blur' }
          ],
          volcentral: [
            { required: true, message: '电压中心值不能为空', trigger: 'blur' }
          ],
          voldown: [
            { required: true, message: '电压下限偏离不能为空', trigger: 'blur' }
          ],
          volup: [
            { required: true, message: '电压上限偏离不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.normid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.normid) {
            this.$http({
              url: this.$http.adornUrl(`/norminfo/norminfo/info/${this.dataForm.normid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.normdescribr = data.norminfo.normdescribr
                this.dataForm.curcentral = data.norminfo.curcentral
                this.dataForm.curdown = data.norminfo.curdown
                this.dataForm.curup = data.norminfo.curup
                this.dataForm.volcentral = data.norminfo.volcentral
                this.dataForm.voldown = data.norminfo.voldown
                this.dataForm.volup = data.norminfo.volup
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/norminfo/norminfo/${!this.dataForm.normid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'normid': this.dataForm.normid || undefined,
                'normdescribr': this.dataForm.normdescribr,
                'curcentral': this.dataForm.curcentral,
                'curdown': this.dataForm.curdown,
                'curup': this.dataForm.curup,
                'volcentral': this.dataForm.volcentral,
                'voldown': this.dataForm.voldown,
                'volup': this.dataForm.volup
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
